import { reqGoodsInfo, reqCardInfo } from "@/api"
import { getNanoid } from '@/util/nanoidToken'

const actions = {
    async goodsList(context, skuId) {
        let result = await reqGoodsInfo(skuId)
        if (result.code === 200) {
            context.commit('GOODSLIST', result.data)
        }
    },
    async addCard(context, { skuId, skuNum }) {
        let result = await reqCardInfo(skuId, skuNum)
        if (result.code === 200) {
            return 'ok'
        }
        else {
            return Promise.reject(new Error('fail'))
        }
    },
}

const mutations = {
    GOODSLIST(state, goodsInfo) {
        state.goodsInfo = goodsInfo
    },
}

const state = {
    goodsInfo: {
    },
    myId: getNanoid()
}

const getters = {
    categoryView(state) {
        return state.goodsInfo.categoryView || {}
    },
    skuInfo(state) {
        return state.goodsInfo.skuInfo || {}
    },
    spuSaleAttrList(state) {
        return state.goodsInfo.spuSaleAttrList || {}
    },

}

export default {
    actions,
    mutations,
    state,
    getters
}